﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VCN.App.DataTransferObjects;
using VCN.App.DataAccessLayer;
using System.Data;

namespace VCN.admin.UC
{
    public partial class slideManageUC : System.Web.UI.UserControl
    {
        #region Fields

        public enum eForm
        {
            insert = 1,
            update = 2
        }
        eForm m_eform = eForm.insert;                
        ImageSlideDAO m_img_dao = new ImageSlideDAO();
        ImageSlide m_img_obj = new ImageSlide();
        DataTable m_dtb; 
        #endregion
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lblStatus.Text = "";
            if (this.IsPostBack == false)
            {
                m_bnt_submit.Text = "Thêm mới";
                getData2Grid();                
            }            
        }

        public void getData2Grid()
        {

            m_dtb = new DataTable();
            int v_int=int.Parse(ddl_Category.SelectedValue.ToString());            
                m_dtb = m_img_dao.SelectByPro(v_int);                            
            m_dt_grv.DataSource = m_dtb;
            m_dt_grv.DataBind();
        }        
        protected void gridProduct_Delete(object sender, GridViewDeleteEventArgs e)
        {
            ImageSlideDAO v_img_dao = new ImageSlideDAO();
            ImageSlide v_img_obj = new ImageSlide();
            v_img_obj.Id = int.Parse(m_dt_grv.DataKeys[e.RowIndex].Value.ToString());
            v_img_dao.deleteByID(v_img_obj);
            this.getData2Grid();
        }
        protected void gridProduct_Edit(object sender, GridViewEditEventArgs e)
        {
            m_bnt_submit.Text = "Cập nhật";            
            int v_id = int.Parse(m_dt_grv.DataKeys[e.NewEditIndex].Value.ToString());
            lblId.Text = v_id.ToString();
            loadObj2Form(v_id);                                                
        }                
        protected void define_e_form()
        {
            if (m_bnt_submit.Text.Equals("Cập nhật"))
            {
                m_eform = eForm.update;
            }
            else m_eform = eForm.insert;
        }
        protected void bnt_upload_click(object sender, EventArgs e)
        {
            try
            {
                define_e_form();
                if (m_eform == eForm.insert)
                {
                    m_img_dao = new ImageSlideDAO();
                    form2Object();
                    m_img_dao.Insert(m_img_obj);
                    lblStatus.Text = "Thêm mới thành công!";
                    getData2Grid();
                }
                else
                {
                    m_img_dao = new ImageSlideDAO();
                    form2Object();
                    m_img_dao.update(m_img_obj);
                    lblStatus.Text = "Cập nhật thành công!";
                    getData2Grid();
                    m_bnt_submit.Text = "Thêm mới";
                }
            }
            catch (Exception v_ex)
            {
            }
        }
        // Gán giá trị từ form vào đối tượng
        public void form2Object()
        {
            //Them cac dinh dang file

            ucupload.ContentFileType = "png, jpg";
            ucupload.EnableRename = false;
            ucupload.SaveToDirectory = "../home/Images/SlideShow/";

            m_img_obj = new ImageSlide();
            // gan vao doi tuong vi  
            if(lblId.Text!="")          
            m_img_obj.Id = int.Parse(lblId.Text.ToString());
            m_img_obj.Order = int.Parse(drdNo.SelectedValue.ToString());
            if (ucupload.HasFile == false)
            {
                m_img_obj.Image = lblChose.Text;
            }
            else
                m_img_obj.Image = ucupload.StartUpload();

        }

        private void loadObj2Form(int ip_Id)
        {
            m_img_dao = new ImageSlideDAO();
            m_img_obj = ImageSlideDAO.Convert(m_img_dao.Select(ip_Id).Rows[0]);
            drdNo.Text = m_img_obj.Order.ToString();
            lblChose.Text = m_img_obj.Image;
        }
   
        protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            getData2Grid();
        }
    }
}